﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Basis.Help
{
    /// <summary>
    /// 字符串帮助工具类
    /// </summary>
    public class StringHelper
    {

        /// <summary>
        /// 判断字符串是否有非空字符
        /// </summary>
        /// <param name="input">需要检测的字符串</param>
        /// <returns></returns>
        public static bool HasText(string input)
        {
            if (!HasLength(input))
            {
                return false;
            }
            foreach (var cr in input.ToCharArray())
            {
                if (!Char.IsWhiteSpace(cr))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断字符串是否有长度
        /// </summary>
        /// <param name="input">需要检测的字符串</param>
        /// <returns></returns>
        public static bool HasLength(string input)
        {
            return input != null && input.Length > 0;
        }

        /// <summary>
        /// 判断字符串是否包含空白符
        /// </summary>
        /// <param name="input">需要检测的字符串</param>
        /// <returns></returns>
        public static bool IsContainsWhitespace(string input)
        {
            if (!HasLength(input))
            {
                return false;
            }
            foreach (char cr in input.ToCharArray())
            {
                if (Char.IsWhiteSpace(cr))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 去除字符串的前后空白符
        /// </summary>
        /// <param name="input">需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimWhitespace(string input)
        {
            if (!HasLength(input))
            {
                return input;
            }
            StringBuilder sb = new StringBuilder(input);
            while (sb.Length > 0 && Char.IsWhiteSpace(sb[0]))
            {
                sb.Remove(0, 1);
            }
            while (sb.Length > 0 && Char.IsWhiteSpace(sb[sb.Length - 1]))
            {
                sb.Remove(sb.Length - 1, 1);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 去除字符串的所有空白符,包括前,后和中间
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static String TrimAllWhitespace(String input)
        {
            if (!HasLength(input))
            {
                return input;
            }
            StringBuilder sb = new StringBuilder(input);
            int index = 0;
            while (sb.Length > index)
            {
                if (Char.IsWhiteSpace(sb[index]))
                {
                    sb.Remove(index, 1);
                }
                else
                {
                    index++;
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 去除字符串的前面的空白符
        /// </summary>
        /// <param name="input">需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimLeadingWhitespace(string input)
        {
            if (!HasLength(input))
            {
                return input;
            }
            StringBuilder sb = new StringBuilder(input);
            while (sb.Length > 0 && Char.IsWhiteSpace(sb[0]))
            {
                sb.Remove(0, 1);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 去除字符串的后面的空白符
        /// </summary>
        /// <param name="input">需要去除的字符串</param>
        /// <returns></returns>
        public static string TrimTrailingWhitespace(string input)
        {
            if (!HasLength(input))
            {
                return input;
            }
            StringBuilder sb = new StringBuilder(input);
            while (sb.Length > 0 && Char.IsWhiteSpace(sb[sb.Length - 1]))
            {
                sb.Remove(sb.Length - 1, 1);
            }
            return sb.ToString();
        }






    }
}
